package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ProcessDevice;
import com.mxpio.erp.common.technology.service.ProcessDeviceService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "ProcessDeviceController", description = "工序信息参数-设备信息接口")
@RestController
@RequestMapping("/erp/tech/procinfo/device")
public class ProcessDeviceController {

    @Autowired
    private ProcessDeviceService service;

    @GetMapping("list")
    @Operation(summary = "工序信息参数-设备列表", description = "获取工序信息参数-设备列表（无分页）", method = "GET")
    public Result<List<ProcessDevice>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProcessDevice> lines = service.list(ProcessDevice.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "工序信息参数-设备列表", description = "获取工序信息参数-设备列表（分页）", method = "GET")
    public Result<Page<ProcessDevice>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProcessDevice> lines = service.listPage(ProcessDevice.class, page, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{processCode}")
    @Operation(summary = "根据processCode获取工序信息参数-设备", description = "根据processCode获取工序信息参数-设备", method = "GET")
    public Result<List<ProcessDevice>> listByRoutProcId(@Parameter(description="业务单号") @PathVariable(name = "processCode", required = true) String processCode) {
        Criteria c = Criteria.create().addCriterion("processCode", Operator.EQ, processCode);
        List<ProcessDevice> lines = service.list(ProcessDevice.class,c);
        return Result.OK(lines);
    }

    @PostMapping("save")
    @Operation(summary = "保存工序参数-设备信息", description = "保存工序参数-设备信息", method = "POST")
    public Result<List<ProcessDevice>> add(@RequestBody List<ProcessDevice> prodTecRoutProcDevices) {
        service.save(prodTecRoutProcDevices);
        return Result.OK(prodTecRoutProcDevices);
    }

    /*@PostMapping("add")
    @Operation(summary = "新增工序参数-设备信息", description = "新增工序参数-设备信息", method = "POST")
    public Result<ProcessDevice> add(@RequestBody ProcessDevice prodTecRoutProcDevice) {
        service.save(prodTecRoutProcDevice);
        return Result.OK(prodTecRoutProcDevice);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑工序参数-设备信息", description = "编辑工序参数-设备信息", method = "PUT")
    public Result<ProcessDevice> edit(@RequestBody ProcessDevice prodTecRoutProcDevice) {
        service.update(prodTecRoutProcDevice);
        return Result.OK(prodTecRoutProcDevice);
    }*/

    @DeleteMapping("remove/{id}")
    @Operation(summary = "删除工序参数-设备信息", description = "删除工序参数-设备信息", method = "DELETE")
    public Result<Item> remove(@PathVariable(name = "id", required = true) String processCode) {
        String[] ids = processCode.split(",");
        for(String key : ids){
            service.delete(ProcessDevice.class, key);
        }
        return Result.OK();
    }
}
